
var app=new Vue({
    data: {
        menu1: 'first',
        menu2: 'article/getWant.do',
        isLogin: false,
        count: 4,
        maxCount: 10,
        centerDialogVisible: false,
        username: "",
        password: "",
        alertMessage: "",
        captcha: "",
        articles: [],
        allArticles:[],
        articleKinds:[],
        currentArticleKind:'',
        articleCount:0,//当前页数
        articleShow:5,//每页显示的文章数
        articleStart:0//当前页开始的文章
    },
    methods: {
        handleClick:function (tab,event){
            let url = this.menu2;
            let article={};
            if (this.currentArticleKind!=''){
               article= {
                    articleKind: {
                        articleKindId: this.currentArticleKind
                    }
                }
            }
            axios.post(url,article).then(resp => {
                    this.articles = resp.data;
                    this.maxCount=this.articles.length;
            });
        },
        changeArticle:function (){
            let articlePage1= this.allArticles.length % this.articleShow!==0?1:0;
            let articlePage=parseInt(this.allArticles.length/this.articleShow)+articlePage1;
            this.articleCount++;
            if(this.articleCount>articlePage){
                this.articleCount=0;
            }
            this.articleStart=(this.articleCount)*this.articleShow;
            alert("1:"+articlePage+"2:"+this.articleCount+"3:"+this.articleStart)
        },
        getArticleByKind:function (articleKindId){
            var article={
                "articleKind":{
                    "articleKindId":articleKindId
                }
            }
            alert(JSON.stringify(article))
            axios.post("article/getWant.do",article).then(resp => {
                    this.articles = resp.data;
                    this.maxCount=this.articles.length;
            });
            this.currentArticleKind=articleKindId;
        },
        load: function () {
            if (this.count <= this.maxCount) {
                this.count += 4
            }
        },
        open: function () {
            this.centerDialogVisible = true;
        },
        changeCaptcha: function () {
            console.log(document.getElementById('captcha'));
            document.getElementById('captcha').src = 'captcha.do?t=' + new Date()
        },

        login: function () {
            axios.get("user/checkCaptcha.do?captcha=" + this.captcha).then(resp => {
                if (resp.data !== true) {
                    this.alertMessage = '验证码错误';
                    this.error();
                } else {
                    console.log("checkCaptcha true")
                    axios.post("user/login.do", {
                        userName: this.username,
                        userPassword: this.password
                    }).then(resp => {
                        // console.log("login",resp);
                        let user = resp.data;
                        if (user.userId == null) {
                            this.alertMessage = '账号或者密码错误';
                            this.error();
                        } else {
                            this.alertMessage = '登录成功';
                            this.success();
                            // window.setTimeout(()=>{},3000);
                            window.location.reload();
                        }
                    });
                }
            });
        },
        error: function () {
            this.$message({
                showClose: true,
                message: this.alertMessage,
                type: 'error'
            });
        },
        success: function () {
            this.$message({
                showClose: true,
                message: this.alertMessage,
                type: 'success'
            });
        },
        openArticle:function (articleId){
            // alert("openArticle");
            window.location.href='read.jsp?articleId='+articleId;
        }
    },
    mounted(){
        alert("isLogin"+this.isLogin);
        if (document.getElementById("userId").value!=null){
            this.isLogin=true;
        }

        axios.post("article/getWant.do",{}).then(resp => {
            this.articles = resp.data;
            this.allArticles=resp.data
            this.maxCount=this.articles.length;
        });
        axios.get("article/getKind.do").then(resp => {
            this.articleKinds = resp.data;
        });
    }
}).$mount("#app");

window.onload=function (){
}